﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace TeacherHelp.FormList.Admin
{
    public partial class AdminMainForm : BaseForm
    {
        #region 子窗体业务处理
        public UserInfoShowForm userInfoShowForm = null;
        public UserAddForm userAddForm = null;
        public UserEditForm userEditForm = null;
        public UserRemoveForm userRemoveForm = null;

        private void ChildFormInt()
        {
            userInfoShowForm = new UserInfoShowForm(this);
            userAddForm = new UserAddForm(this);
        }

        /// <summary>
        /// 子窗体展示
        /// </summary>
        /// <typeparam name="T">窗体类型</typeparam>
        /// <param name="form">窗体实例对象</param>
        public void child_ShowForm<T>(T form) where T : Form
        {
            //this.IsMdiContainer = true;//父窗体
            //panelContent.Dock = DockStyle.Fill;
            contetnPanel.Controls.Clear();

            form.MdiParent = this;
            form.Dock = DockStyle.Fill;
            form.StartPosition = FormStartPosition.CenterScreen;
            form.WindowState = FormWindowState.Normal;
            form.Parent = contetnPanel;

            form.Show();
        }
        #endregion

        public AdminMainForm()
        {
            InitializeComponent();
            ChildFormInt();
        }

        private void AdminMainForm_Load(object sender, EventArgs e)
        {
            loadTimer.Start();
            timeTimer.Start();
            child_ShowForm(userInfoShowForm);
        }

        public void ShowUserInfoForm()
        {
            this.userInfoShowForm = new UserInfoShowForm(this);
            this.child_ShowForm(this.userInfoShowForm);
        }

        private void CloseBtn_Click(object sender, EventArgs e)
        {
            Application.ExitThread();
        }

        private void MinBtn_Click(object sender, EventArgs e)
        {
            this.WindowState = FormWindowState.Minimized;
        }

        private void MaxBtn_Click(object sender, EventArgs e)
        {
            //窗体最大化 最小化
            if (this.WindowState == FormWindowState.Maximized)
            {
                this.MaxBtn.BackgroundImage = global::TeacherHelp.Properties.Resources.fullscreen_expand;
                this.WindowState = FormWindowState.Normal;
            }
            else
            {
                this.FormBorderStyle = FormBorderStyle.None;
                this.MaximumSize = new Size(Screen.PrimaryScreen.WorkingArea.Width, Screen.PrimaryScreen.WorkingArea.Height);
                this.WindowState = FormWindowState.Maximized;
                this.MaxBtn.BackgroundImage = global::TeacherHelp.Properties.Resources.fullscreen_shrink;
            }
        }

        private void panelTitle_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                //窗体拖动
                ReleaseCapture(); SendMessage(Handle, 0xA1, 0x02, 0);
            }
        }

        private void AddUserBtn_Click(object sender, EventArgs e)
        {
            userAddForm = new UserAddForm(this);
            child_ShowForm(userAddForm);
        }

        private void button3_Click(object sender, EventArgs e)
        {
            userInfoShowForm = new UserInfoShowForm(this);
            child_ShowForm(userInfoShowForm);
        }

        private void EditUserBtn_Click(object sender, EventArgs e)
        {
            UserEdit();
        }

        public void UserEdit()
        {
            var selectVaule = userInfoShowForm.UserInfoGridView.SelectedRows;
            if (selectVaule.Count == 0)
            {
                MessageBox.Show("请选择行！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }

            var userId = selectVaule[selectVaule.Count - 1].Cells[nameof(ViewModel.User.Id)].Value.ToInt();
            userEditForm = new UserEditForm(this, userId);
            child_ShowForm(userEditForm);
        }

        private void DeleteUserBtn_Click(object sender, EventArgs e)
        {
            var selectVaule = userInfoShowForm.UserInfoGridView.SelectedRows;
            if (selectVaule.Count == 0)
            {
                MessageBox.Show("请选择行！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            var role = selectVaule[0].Cells[nameof(ViewModel.User.roleName)].Value.ToString();
            if (role == "超级管理员")
            {
                MessageBox.Show("超级管理员不允许删除！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return;
            }

            var userId = selectVaule[0].Cells[nameof(ViewModel.User.Id)].Value.ToInt();
            userRemoveForm = new UserRemoveForm(this, userId);
            child_ShowForm(userRemoveForm);
        }

        private void timeTimer_Tick(object sender, EventArgs e)
        {
            this.thisTimeLable.Text = "当前时间：" + DateTime.Now;
        }

        private void loadTimer_Tick(object sender, EventArgs e)
        {
            if (Opacity == 1)
            {
                loadTimer.Stop();
            }
            else
            {
                Opacity += 0.1;
            }
        }

        private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Application.ExitThread();
        }


        private void teacherNotifyIcon_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            this.WindowState = FormWindowState.Normal;
        }
    }
}
